package com.cloudkinto.service.consumer.orderRegular;

import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.service.platform.PlatformOrderService;
import com.cloudkinto.service.regular.PlatformOrderRegularService;
import com.cloudkinto.service.regular.vo.PlatformOrderCheckReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderRegularConsumer {
    private final PlatformOrderRegularService platformOrderRegularService;
    private final PlatformOrderService platformOrderService;


    /**
     * 自动标发
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Auto_MarkShip))
    public void autoMarkShip(String msg) {
        Map<String, Object> req = JSONObject.parseObject(msg, Map.class);
        try {
            platformOrderService.markShip(req, "auto", null, Long.parseLong(req.get("companyId").toString()), Long.parseLong(req.get("tenantId").toString()));
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 自动审核
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Auto_distributeWh), concurrency = "1", containerFactory = "singleConsumerFactory")
    public void autoCheck(String msg) {
        Map<String, Object> req = JSONObject.parseObject(msg, Map.class);
        try {
            platformOrderService.check(req, null, Long.parseLong(req.get("companyId").toString()), Long.parseLong(req.get("tenantId").toString()));
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 异步分配物流渠道 仓库，并提交仓库
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_distributeWh), concurrency = "1", containerFactory = "singleConsumerFactory")
    public void orderRule(String msg) {
        PlatformOrderCheckReq req = JSONObject.parseObject(msg, PlatformOrderCheckReq.class);
        try {
            platformOrderRegularService.orderRule(req);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }




}
